package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgUserBargainDetailMapper;
import com.ygqh.baby.po.YgProduct;
import com.ygqh.baby.po.YgUserBargainDetail;
import com.ygqh.baby.po.YgUserBargainDetailExample;
import com.ygqh.baby.service.YgUserBargainDetailService;
import com.ygqh.baby.service.YgUserBargainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Service
public class YgUserBargainDetailServiceImpl implements YgUserBargainDetailService {

    @Autowired
    private YgUserBargainDetailMapper ygUserBargainDetailMapper;
    @Autowired
    private YgUserBargainService ygUserBargainService;

    @Override
    public List<YgUserBargainDetail> find() {
        YgUserBargainDetailExample example = new YgUserBargainDetailExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        return ygUserBargainDetailMapper.selectByExample(example);
    }

    @Override
    public int findCount() {
        YgUserBargainDetailExample example = new YgUserBargainDetailExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        return ygUserBargainDetailMapper.countByExample(example);
    }

    @Override
    public YgUserBargainDetail findById(Long id) {
        return ygUserBargainDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public int save(YgUserBargainDetail ygUserBargainDetail) {
        return ygUserBargainDetailMapper.insertSelective(ygUserBargainDetail);
    }

    @Override
    public int update(YgUserBargainDetail ygUserBargainDetail) {
        return ygUserBargainDetailMapper.updateByPrimaryKeySelective(ygUserBargainDetail);
    }

    @Override
    public ResultSet<YgUserBargainDetail> search(QueryInfo queryInfo, String q) {
        List<YgUserBargainDetail> list = ygUserBargainDetailMapper.selectSuper(queryInfo, q);
        int count = ygUserBargainDetailMapper.countSuper(q);
        ResultSet<YgUserBargainDetail> resultSet = new ResultSet<YgUserBargainDetail>(count, list);
        return resultSet;
    }

    @Override
    public int deleteSoft(Long[] ids) {
        Assert.notEmpty(ids);
        YgUserBargainDetailExample example = new YgUserBargainDetailExample();
        example.createCriteria().andIdIn(Arrays.asList(ids));
        YgUserBargainDetail record = new YgUserBargainDetail();
        record.setStatus(DataStatus.Delete);

        return ygUserBargainDetailMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int deleteSoft(Long id) {
        return this.deleteSoft(new Long[]{id});
    }

    @Override
    public int saveOrUpdate(YgUserBargainDetail ygUserBargainDetail) {
        int r = 0;
        if (ygUserBargainDetail.getId() != null) {
            r = this.update(ygUserBargainDetail);
        } else {
            r = this.save(ygUserBargainDetail);
        }
        return r;
    }

    @Override
    public YgUserBargainDetail addUserBargainDetail(Long userId, String openId, String headUrl, String nickName, Long userBargainId, BigDecimal cutPrice) {
        YgUserBargainDetail detail = new YgUserBargainDetail(openId, nickName, headUrl, userBargainId, cutPrice);
        detail.setUserId(userId);
        this.save(detail);
        return detail;

    }

    @Override
    public Boolean alreadyCut(String openId, Long userBargainId) {
        YgUserBargainDetailExample example = new YgUserBargainDetailExample();
        YgUserBargainDetailExample.Criteria criteria = example.createCriteria()
                .andOpenIdEqualTo(openId)
                .andStatusEqualTo(DataStatus.Valid.name());
        if (userBargainId != null) {
            criteria.andUserBargainIdEqualTo(userBargainId);
        }
        int i = ygUserBargainDetailMapper.countByExample(example);
        return i > 0;
    }

    @Override
    public List<YgUserBargainDetail> findCutList(Long bargainId) {
        YgUserBargainDetailExample example = new YgUserBargainDetailExample();
        example.createCriteria().andUserBargainIdEqualTo(bargainId).andStatusEqualTo(DataStatus.Valid.name());
        return ygUserBargainDetailMapper.selectByExample(example);
    }

    @Override
    public List<YgProduct> findProductListByBargainId(Long bargainId) {
        return ygUserBargainDetailMapper.selectProductByBargainId(bargainId);
    }

}
